package com.dc.project.workflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dc.common.annotation.DataScope;
import com.dc.common.constant.WorkflowConstants;
import com.dc.common.exception.ServiceException;
import com.dc.common.utils.PageUtil;
import com.dc.common.vo.ResultVo;
import com.dc.project.workflow.dao.WfModelCategoryDao;
import com.dc.project.workflow.dto.WfModelCategoryDto;
import com.dc.project.workflow.entity.WfCategory;
import com.dc.project.workflow.entity.WfModelCategory;
import com.dc.project.workflow.service.WfModelCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * 流程模型分类关联表(WfModelCategory)表服务实现类
 *
 * @author zhuangcy
 * @since 2021-06-30 14:16:37
 */
@Service("wfModelCategoryService")
public class WfModelCategoryServiceImpl extends ServiceImpl<WfModelCategoryDao, WfModelCategory> implements WfModelCategoryService {

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return
     */
    @Override
    public ResultVo queryById(Serializable id) {
        return ResultVo.success().data(this.getById(id));
    }

    /**
     * 分页查询流程模型分类关联表
     *
     * @param dto
     * @return
     */
    @DataScope
    @Override
    public ResultVo queryByPage(WfModelCategoryDto dto) {
        QueryWrapper<WfModelCategory> queryWrapper = new QueryWrapper<>();
        IPage<WfModelCategory> page = this.page(PageUtil.convert2PageParam(dto), queryWrapper);
        return ResultVo.success().data(PageUtil.convert2PageResult(page));
    }

    /**
     * 新增流程模型分类关联表
     *
     * @param wfModelCategory
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo add(WfModelCategory wfModelCategory) {
        settingDefault(wfModelCategory);
        if (this.save(wfModelCategory)) {
            return ResultVo.success();
        }
        throw new ServiceException();
    }

    /**
     * 编辑流程模型分类关联表
     *
     * @param wfModelCategory
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo edit(WfModelCategory wfModelCategory) {
        WfModelCategory nowCategory = this.getById(wfModelCategory.getMcId());
        if (null == nowCategory) {
            throw new ServiceException();
        }
        if (!nowCategory.getDefValue().equals(wfModelCategory.getDefValue())) {
            settingDefault(wfModelCategory);
        }
        if (this.updateById(wfModelCategory)) {
            return ResultVo.success();
        }
        throw new ServiceException();
    }

    private void settingDefault(WfModelCategory wfModelCategory) {
        if (WorkflowConstants.ENABLED_STATE.equals(wfModelCategory.getDefValue())) {
            UpdateWrapper<WfModelCategory> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda()
                    .set(WfModelCategory::getDefValue, WorkflowConstants.DISABLED_STATE)
                    .eq(WfModelCategory::getCategoryId, wfModelCategory.getCategoryId());
            this.update(updateWrapper);
        } else {
            wfModelCategory.setDefValue(WorkflowConstants.DISABLED_STATE);
        }
    }

    /**
     * 根据主键删除流程模型分类关联表
     *
     * @param id 主键
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo delete(Serializable id) {
        return ResultVo.verify(this.removeById(id));
    }

    /**
     * 根据类别查询流程模型id
     *
     * @param docType
     * @return
     */
    @Override
    public String getCategoryModelIdByType(String docType) {
        return baseMapper.getCategoryModelIdByType(docType);
    }

    /**
     * 根据类别查询流程对应接口名
     *
     * @param category
     * @return
     */
    @Override
    public WfCategory getClassNameByCategory(String category) {
        return baseMapper.getClassNameByCategory(category);
    }

    /**
     * 删除模型ID删除关联信息
     *
     * @param modelId
     */
    @Override
    public void deleteByModelId(String modelId) {
        LambdaQueryWrapper<WfModelCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WfModelCategory::getModelId, modelId);
        this.remove(queryWrapper);
    }
}